using imitateMacQQ.WebApi.Domain.real_time.communication.Entity;
using imitateMacQQ.WebApi.Domain.real_time.communication.Interface;
using imitateMacQQ.WebApi.Domain.Request.utils;
using imitateMacQQ.WebApi.Repository.EFCore.Model;
using imitateMacQQ.WebApi.Repository.Redis;
using Microsoft.EntityFrameworkCore;


namespace imitateMacQQ.WebApi.Domain.real_time.communication.Service;

public class DesignService : IDesign
{
    public DesignService()
    {
        // _db = db;
    }

    // private readonly DataDbContext _db;

    public async Task<Request<GetPipeline[]>> GetIngo(InfoAccount account)
    {
        using DataDbContext _db = new DataDbContext();
        var info = _db.RealTimeInfos
            .Where(e =>
                e.User2Account == account.Account2
                && e.User1Account == account.Account1)
            .ToList();
        var list = new GetPipeline[info.Count];
        foreach (var item in info)
        {
            var index = info.IndexOf(item);
            list[index] = new GetPipeline()
            {
                UserSendAccount = item.User1Account,
                Text = item.Text,
                DateTime = item.DateTime
            };
        }

        foreach (var item in info)
        {
            _db.RealTimeInfos.Remove(item);
        }

        await _db.SaveChangesAsync();
        // var tempData = RedisProtect.Del(account.Account2 + "inf" ?? "NULL");
        return Request<GetPipeline[]>.Ok(list);
    }

    public async Task<Request<bool>> SendMessageOfUser(SavePipeline message)
    {
        try
        {
            using DataDbContext _db = new DataDbContext();
            await _db.RealTimeInfos.AddAsync(new RealTimeInfo()
            {
                User1Account = message.UserSendAccount,
                User2Account = message.UserAcceptAccount,
                User2Reader = false,
                Text = message.Text,
                DateTime = DateTime.Now
            });
            await _db.SaveChangesAsync();
        }
        catch
        {
            return Request<bool>.Ok(false, false, "Database exception");
        }

        return Request<bool>.Ok(true);
    }

    public Request<bool> GetInfoResult(InfoAccount res)
    {
        try
        {
            if (string.IsNullOrEmpty(res.Account2))
            {
                using DataDbContext _db = new DataDbContext();
                var delData = _db.RealTimeInfos.Where(e =>
                    e.User1Account == res.Account1 &&
                    e.User2Account == res.Account2);
                foreach (var item in delData)
                {
                    _db.RealTimeInfos.Remove(item);
                }

                _db.SaveChanges();
                var redisData = RedisProtect.Get(res.Account2 + "inf");
                redisData = redisData.Replace(res.Account1 + "inf", "");
                RedisProtect.Set(res.Account2 ?? "NULL", redisData);
            }
        }
        catch
        {
            return Request<bool>.Ok(false, false, "Database exception");
        }

        return Request<bool>.Ok(true);
    }

    public async Task<Request<DateTime>> GetInfoEndTime(GetInfoTime dto)
    {
        using DataDbContext _db = new DataDbContext();
        var res = await _db.RealTimeInfos.FirstOrDefaultAsync(e =>
            e.User1Account == dto.Account1 && e.User2Account == dto.Account2);
        return Request<DateTime>.Ok(res?.DateTime ?? DateTime.Now);
    }
}